package per.edward.wizard.ext;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class WizardPanelProblemDisplayer extends JPanel {

    public static final int PROBLEM_INFO = 1;

    public static final int PROBLEM_WARN = 2;

    public static final int PROBLEM_ERROR = 3;

    private ImageIcon[] images_;

    private JLabel imageLabel_;

    private JLabel messageLabel_;

    public WizardPanelProblemDisplayer(Component panel) {
        JPanel problem = new JPanel(new FlowLayout(FlowLayout.LEFT));

        loadImages();
        imageLabel_ = new JLabel();
        messageLabel_ = new JLabel();

        problem.add(imageLabel_);
        problem.add(messageLabel_);

        setLayout(new BorderLayout());

        add(panel, BorderLayout.CENTER);
        add(problem, BorderLayout.SOUTH);
    }

    public void clear() {
        imageLabel_.setIcon(null);
        messageLabel_.setText("");
    }

    public void setProblem(Object source, int level, String message) {
        if (level != PROBLEM_INFO && level != PROBLEM_WARN
                && level != PROBLEM_ERROR) {
            throw new IllegalArgumentException(
                    "Problem level should be one of PROBLEM_INFO, PROBLEM_WARN, and PROBLEM_ERROR.");
        }

        imageLabel_.setIcon(images_[level]);
        messageLabel_.setText(message);
    }

    private void loadImages() {

        images_ = new ImageIcon[4];
        images_[0] = null;

        images_[PROBLEM_INFO] = new ImageIcon(getClass().getClassLoader()
                .getResource("resources/images/info.png"));
        images_[PROBLEM_WARN] = new ImageIcon(getClass().getClassLoader()
                .getResource("resources/images/warn.png"));
        images_[PROBLEM_ERROR] = new ImageIcon(getClass().getClassLoader()
                .getResource("resources/images/error.png"));
    }
}
